package com.ruoyi.pcj.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mashuai.utils.toolkit.core.msHandlebean.MsHandleDataUtils;
import com.mashuai.utils.toolkit.core.msMp.MsDb;
import com.mashuai.utils.toolkit.core.msValidation.annotation.MsCheckParams;
import com.ruoyi.basis.service.MsBaseServiceImpl;
import com.ruoyi.pcj.service.IPcjHobbyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.pcj.mapper.PcjHobbyTypeMapper;
import com.ruoyi.pcj.domain.PcjHobbyType;
import com.ruoyi.pcj.service.IPcjHobbyTypeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 兴趣类型Service业务层处理
 *
 * @author ms
 * @date 2024-02-03
 */
@AllArgsConstructor
@Service
public class PcjHobbyTypeServiceImpl extends MsBaseServiceImpl<PcjHobbyTypeMapper, PcjHobbyType> implements IPcjHobbyTypeService {

    private IPcjHobbyService pjcHobbyService;

    /**
     * 查询兴趣类型
     *
     * @param id 兴趣类型主键
     * @return 兴趣类型
     */
    @Override
    public PcjHobbyType selectPcjHobbyTypeById(String id) {
        PcjHobbyType entity = MsDb.getById(id, PcjHobbyType.class);
        MsHandleDataUtils.handleTbOrDictValBeans(entity, PcjHobbyType.class);
        return entity;
    }

    /**
     * 查询兴趣类型列表
     *
     * @param pcjHobbyType 兴趣类型
     * @return 兴趣类型
     */
    @MsCheckParams
    @Override
    public List<PcjHobbyType> selectPcjHobbyTypeList(PcjHobbyType pcjHobbyType) {
        return baseMapper.selectPcjHobbyTypeList(pcjHobbyType);
    }

    /**
     * 新增兴趣类型
     *
     * @param pcjHobbyType 兴趣类型
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertPcjHobbyType(PcjHobbyType pcjHobbyType) {
        Boolean success = this.insertData(pcjHobbyType);
        //子表
        pjcHobbyService.batchSaveHobby(pcjHobbyType.getHobbyList(), pcjHobbyType.getId());
        return success;
    }

    /**
     * 修改兴趣类型
     *
     * @param pcjHobbyType 兴趣类型
     * @return 结果
     */
    @Override
    public Boolean updatePcjHobbyType(PcjHobbyType pcjHobbyType) {
        Boolean success = this.updateData(pcjHobbyType);
        pjcHobbyService.batchSaveHobby(pcjHobbyType.getHobbyList(), pcjHobbyType.getId());
        return success;
    }


    /**
     * 批量删除兴趣类型
     *
     * @param ids 需要删除的兴趣类型主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deletePcjHobbyTypeByIds(String[] ids) {
        //如果数据被其他表引用，禁止删除
        this.delCheck(ids, PcjHobbyType.class);
        //如果不存在引用进行删除
        Boolean success = this.removeBatchByIds(Arrays.asList(ids));
        return success;

    }

    @Override
    public List<PcjHobbyType> getHobbyData() {
        LambdaQueryWrapper<PcjHobbyType> qw = Wrappers.lambdaQuery();
        qw.orderByAsc(PcjHobbyType::getSeqNo);
        List<PcjHobbyType> list = baseMapper.selectList(qw);
        MsHandleDataUtils.handleTbOrDictValBeans(list, PcjHobbyType.class);
        return list;
    }
}
